package com.qilin.hscs.udp;

import com.qilin.hscs.redis.JedisTemplate;
import com.qilin.hscs.util.UUIDUtil;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.Scanner;
import java.util.UUID;

/**
 * Created by 10188 on 2018/4/26.
 */
public class UDPserver {

    private  final int prot = 8888;
    private  JedisTemplate template=new JedisTemplate();
    private  DatagramSocket datagramSocket;
    private  int targetPort;
    private String IEEE="";
    private String pack;
    private  InetAddress targetHost;
    private  final String IEEEName = "hostIEEE";
    private static final String SECRET = "QiLin";
    private  void send() {
        getIEEE();
        byte[] bytes = pack.getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        datagramPacket.setSocketAddress(new InetSocketAddress(targetHost,targetPort));
        try {
            datagramSocket.send(datagramPacket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private  void receive() {
        try {
            datagramSocket = new DatagramSocket(prot, InetAddress.getByName("192.168.31.141"));
            byte b[] = new byte[1024];
            DatagramPacket datagramPacket = new DatagramPacket(b, 1024);
            while (true) {
                datagramSocket.receive(datagramPacket);
                targetPort=datagramPacket.getPort();
                System.out.println(targetPort);
                targetHost=datagramPacket.getAddress();
                System.out.println(targetHost.getHostAddress());
                byte[] data = datagramPacket.getData();
                String secret = new String(data, "utf-8").trim();
                System.out.println(secret);
                if (secret.equals(SECRET)) {
                    send();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("udp服务器断开连接，正在尝试重新连接");
        }
    }
    public  static void main(String[] args) {
        UDPserver udPserver=new UDPserver();
        udPserver.IEEEBomb();
        udPserver.receive();
    }

    public void getIEEE() {
        try {

            if (template.isExist(IEEEName)) {
                IEEE = template.get(IEEEName);
                pack = "QiLin" + IEEE + "192.168.31.141";
            } else {
                System.out.println("请重新启动整个服务器");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void  IEEEBomb(){
        template.connect();
        try {
            long start = System.currentTimeMillis();
            Process process = Runtime.getRuntime().exec(
                    new String[] { "wmic", "cpu", "get", "ProcessorId" });
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String property = sc.next();
            String serial = sc.next();
            IEEE=serial;
            Boolean is = template.set(IEEEName, IEEE);
                pack = IEEE+Inet4Address.getLocalHost().getHostAddress();
                if (!is) {
                    System.out.println("无法缓存，系统错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
        }
    }
}

